<?php

namespace app\shop\model;

class Promotion extends \ShopModel
{
    protected $table = 'sh_promotion';


    // 添加促销活动
    public function addpromotion($postdata)
    {
    	//$postdata["cus_level"]=$this->cusLevelToId($postdata["cus_level"]);
        return $this->insertAll($postdata);
    }

     //客户等级转换
    public function cusLevelToId($levelstr){
        $levelarr=json_decode($levelstr,true);
        $sqlstr=[];
        foreach ($levelarr as $k => $v) {
            # code...
            $sqlstr[]="'{$v}'";
        }
        if($sqlstr){
        	$sqlstr=implode(',', $sqlstr);
        	$sqlstr=db("client_level")->where("level_name in ({$sqlstr})")->column("level_id");
        }
        return implode(',',$sqlstr);
        
       
    }
    //获取促销活动列表
    public function promotionList($where,$page,$list){
    	$prolist=$this->where($where)->limit(($page-1)*$list,$list)->select();
    	return $prolist;
    }
    //获取总数
    public function getpronum($where,$page,$list){
    	$prolist=$this->where($where)->count();
    	if(!$prolist) $prolist=0;
    	return $prolist;
    }
    //作废
    public function fei($pids){
    	$back=$this->where("promotion_id in ($pids)")->update(["is_delete"=>1]);
    	return $back;
    }
    //删除
    public function shan($pids){
    	$back=$this->where("promotion_id in ($pids)")->delete();
    	return $back;
    }
}
